// pages/search_keyword/search_keyword.js
const appInstance = getApp()
const getInf = (str, key) => str.replace(new RegExp(`${key}`, 'g'), `%%${key}%%`).split('%%');
var Value = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: '取消',
    active: 0,
    classifyList: [],
    videoList: [],
    classifyLists: [],
    inpValue:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options.keyword)
    var keyword = options.keyword;
    this.AllvideoList(keyword);
    this.classifyList();
    this.setData({
      inpValue:keyword
    })
    // this.tapName();
  },

  //全部视频列表
  AllvideoList(keyword) {
    var that = this;
    wx.request({
      url: appInstance.globalData.url + '/video/list',
      method: 'POST',
      data: {
        keyword: keyword
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.data)
        that.setData({
          videoList: res.data.data
        })
      },

    })
  },

  //分类列表
  classifyList() {
    var that = this;
    wx.request({
      url: appInstance.globalData.url + '/category/list',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.data)
        that.setData({
          classifyList: res.data.data
        })
      },

    })
  },

  //点击获取分类视频
  onChange(e) {
    console.log(e)
    var Index = e.detail.index;
    // console.log(Index)
    if (Index === 0) {
      return false
    }
    var Id = this.data.classifyList[Index - 1].id
    // console.log(Id)
    var that = this;
    wx.request({
      url: appInstance.globalData.url + '/video/list',
      method: 'POST',
      data: {
        category_id: Id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.data)
        that.setData({
          classifyLists: res.data.data
        })
      },

    })
  },
  //键盘输入完成时触发事件
  tapName(e) {
    // console.log(e.detail.value)
    Value = e.detail.value
    var that = this
    that.setData({
      active:0,
      inpValue: e.detail.value
    })
    wx.request({
      url: appInstance.globalData.url + '/video/list',
      method: 'POST',
      data: {
        keyword: Value
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.data)
        var newData = res.data.data
        newData.forEach(item=>{
          // console.log(item.name)
          var dic = item;
          var NewName = dic['name']
          // console.log(NewName)
          var name = getInf(NewName,that.data.inpValue)
        })
        that.setData({
          videoList: newData
        })
      },

    })

  },

  //点击取消返回首页
  backIndex() {
    wx.switchTab({
      url: '../index/index',
    })
  },

  //跳转到详情
  videoDetail(e) {
    // console.log(e.currentTarget.id)
    var id = e.currentTarget.id
    wx.navigateTo({
      url: '../index/classify/videoDetail/videoDetail?id=' + id,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})